﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DentScan2.DB;

namespace DentScan2.WebApp.Classes
{
    [Serializable]
    public class DamageDetail
    {
        public string DetailKindId
        {
            get;
            set;
        }

        public string DetailName
        {
            get
            {
                return Tools.Tools.GetDetailNameById(DetailKindId);
            }
            set
            {
            }
        }

        public int Size1
        {
            get;
            set;
        }
        public int Size2
        {
            get;
            set;
        }
        public int Size3
        {
            get;
            set;
        }
        public int Size4
        {
            get;
            set;
        }
        public int Minutes
        {
            get;
            set;
        }
        
        public bool ALU
        {
            get;
            set;
        }
        public decimal AWS
        {
            get;
            set;
        }
        public decimal Price
        {
            get;
            set;
        }
        public long Obs
        {
            get;
            set;
        }


        public string ObsStr
        {
            get;
            set;
        }

        public int DisassemblyTime
        {
            get;
            set;
        }

        public int DisassemblyPrice
        {
            get;
            set;
        }

        public Decimal Materials
        {
            get;
            set;
        }
        
        public bool PNP
        {
            get;
            set;
        }
        
        public bool Polish
        {
            get;
            set;
        }

        public bool Traditional
        {
            get;
            set;
        }

        
        public bool WithReserve
        {
            get;
            set;
        }

        public List<DetailPhoto> DetailPhotos
        {
            get;
            set;
        }


        public DamageDetail()
        {
            DetailPhotos = new List<DetailPhoto>();
        }

        public DamageDetail(DB.DamageDetail detail)
        {
            this.ALU = detail.ALU;
            this.AWS = detail.AWS.Value;
            this.DetailKindId = detail.DetailKindId;
            this.DisassemblyTime = detail.DisassemblyTime;
            this.Materials = detail.Materials.HasValue ? detail.Materials.Value : 0;
            this.Obs = detail.Obs;
            this.PNP = detail.PNP;
            this.Polish = detail.Polish;
            this.Price = detail.Price.Value;
            this.Size1 = detail.Size1;
            this.Size2 = detail.Size2;
            this.Size3 = detail.Size3;
            this.Size4 = detail.Size4;
            this.Minutes = detail.Minutes.HasValue ? detail.Minutes.Value : 0;
            //this.JustHours = detail.JustHours;
            this.Traditional = detail.Traditional;
            this.WithReserve = detail.WithReserve;            
            this.ObsStr = detail.ObsStr;
            var photos = detail.DetailPhotos.Where(dp => !dp.IsDel);
            DetailPhotos = new List<DetailPhoto>();
            foreach (var photo in photos)
                this.DetailPhotos.Add(new DetailPhoto(photo));
        }


        public void SaveToDB(DLDataContext dl, Guid damageId)
        {
            Guid newId = Guid.NewGuid();
            DB.DamageDetail dd = new DB.DamageDetail()
            {
                DamageId = damageId,
                DamageDetailId = newId,
                ALU = this.ALU,
                AWS = this.AWS,
                DetailKindId = this.DetailKindId,
                DisassemblyTime = this.DisassemblyTime,
                Materials = this.Materials,
                Obs = this.Obs,
                PNP = this.PNP,
                Polish = this.Polish,
                Price = this.Price,
                Size1 = this.Size1,
                Size2 = this.Size2,
                Size3 = this.Size3,
                Size4 = this.Size4,
                Traditional = this.Traditional,
                IsDel = false,
                LastEditDate = DateTime.UtcNow,                
                Minutes = this.Minutes,
                WithReserve = this.WithReserve,
                ObsStr = this.ObsStr
                //JustHours = this.JustHours
            };

            dl.DamageDetails.InsertOnSubmit(dd);
            dl.SubmitChanges();
            foreach (var dp in dl.DetailPhotos.Where(dp => dp.DamageDetailId == newId).ToArray())
            {
                dp.IsDel = true;
                dp.LastEditDate = DateTime.UtcNow;
            }
            dl.SubmitChanges();
            foreach (var dp in this.DetailPhotos)
                dp.SaveToDB(dl, newId);

        }
    }
}